package com.igeek.shop.web.houtai;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.igeek.shop.entity.Category;
import com.igeek.shop.entity.Product;
import com.igeek.shop.service.ListService;

public class ListServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ListService list = new ListService();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		String state = req.getParameter("state");
		if(state.equals("Display")){
			queryList(req,resp);
		}else if(state.equals("Add")){
			addlist(req,resp);
		}else if(state.equals("Writer")){
			writer(req,resp);
		}else if(state.equals("updateOne")){
			updateOne(req,resp);
		}else if(state.equals("Delect")){
			delect(req,resp);
		}else if(state.equals("SPAdd")){
			spadd(req,resp);
		}
		
	}
	
	private void spadd(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		//商品名称
		String pname = req.getParameter("pname");
		System.out.println(pname);
		//是否热门
		String is_hot =req.getParameter("is_hot");
		System.out.println(is_hot);
		//市场价格
		String market_price =req.getParameter("market_price");
		System.out.println(market_price);
		//商城价格
		String shop_price =req.getParameter("shop_price");
		System.out.println(shop_price);
		//商品图片
		String upload =req.getParameter("upload");
		System.out.println(upload );
		//所属分类
		String categorySecond=req.getParameter("categorySecond.csid");
		System.out.println(categorySecond);
		//商品描述
		String pdesc =req.getParameter("pdesc");
		System.out.println(pdesc);
	}
	private void delect(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		String cid =req.getParameter("cid");
		list.delect(cid);
		System.out.println(cid);
		queryList(req,resp);
	}
	private void updateOne(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		String hiddenId = (String)req.getParameter("hiddenId");
		String cname = req.getParameter("cname");
		list.writer(hiddenId, cname);
		queryList(req,resp);
	}
	private void writer(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		String cid = req.getParameter("cid");
		Category cate = query(cid);
		req.setAttribute("cate", cate);
		try {
			req.getRequestDispatcher("/admin/category/edit.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private void addlist(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		String cname = (String)req.getParameter("cname");
		list.addList(cname);
		System.out.println(cname);
		queryList(req,resp);
	}
	private void queryList(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		List<Category> cate = list.querylist();
		req.setAttribute("cate", cate);
		try {
			req.getRequestDispatcher("/admin/category/list.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Category  query(String cid){
		 Category cate= (Category) list.query(cid);
		return cate;
		
	}
	

	
	
}
